package com.gen.utils.lan;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;

public class Hangul {
    public static class KSyllable {
        public final int initial;

        public final int medial;

        public final int finale;


        public KSyllable(int initial, int medial, int finale) {
            this.initial = initial;
            this.medial = medial;
            this.finale = finale;
        }

    }

    private static Map<Integer, KSyllable> allSyllables = generateAllSyllables();
    private static Map<Integer, String> allInitial ;
    private static Map<Integer, String> allMedial;
    private static Map<Integer, String> allFinal;
    static {
        allInitial = new HashMap<>();
//        allInitial.put(0,"g");
        allInitial.put(0,"k");

        allInitial.put(1,"kk");
        allInitial.put(2,"n");
        allInitial.put(3,"d");
        allInitial.put(4,"tt");
        allInitial.put(5,"r");
        allInitial.put(6,"m");
        allInitial.put(7,"b");
        allInitial.put(8,"pp");
        allInitial.put(9,"s");
        allInitial.put(10,"ss");
        allInitial.put(11,"");
        allInitial.put(12,"j");
        allInitial.put(13,"jj");
        allInitial.put(14,"ch");
        allInitial.put(15,"k");
        allInitial.put(16,"t");
        allInitial.put(17,"p");
        allInitial.put(18,"h");

        allMedial = new HashMap<>();
        allMedial.put(0,"a");
        allMedial.put(1,"ae");
        allMedial.put(2,"ya");
        allMedial.put(3,"yae");
        allMedial.put(4,"eo");
        allMedial.put(5,"e");
        allMedial.put(6,"yeo");
        allMedial.put(7,"ye");
        allMedial.put(8,"o");
        allMedial.put(9,"wa");
        allMedial.put(10,"wae");
        allMedial.put(11,"oe");
        allMedial.put(12,"yo");
        allMedial.put(13,"u");
        allMedial.put(14,"wo");
        allMedial.put(15,"we");
        allMedial.put(16,"wi");
        allMedial.put(17,"yu");
        allMedial.put(18,"eu");
        allMedial.put(19,"ui");
        allMedial.put(20,"i");

        allFinal= new HashMap<>();
        allFinal.put(0,"");
        allFinal.put(1,"g");
        allFinal.put(2,"kk");
        allFinal.put(3,"gs");
        allFinal.put(4,"n");
        allFinal.put(5,"nj");
        allFinal.put(6,"nh");
        allFinal.put(7,"d");
        allFinal.put(8,"l");
        allFinal.put(9,"rg");
        allFinal.put(10,"rm");
        allFinal.put(11,"rb");
        allFinal.put(12,"rs");
        allFinal.put(13,"rt");
        allFinal.put(14,"rp");
        allFinal.put(15,"rh");
        allFinal.put(16,"m");
        allFinal.put(17,"b");
        allFinal.put(18,"bs");
        allFinal.put(19,"s");
        allFinal.put(20,"ss");
        allFinal.put(21,"ng");
        allFinal.put(22,"j");
        allFinal.put(23,"ch");
        allFinal.put(24,"k");
        allFinal.put(25,"t");
        allFinal.put(26,"p");
        allFinal.put(27,"h");



    }

    public static void main(String[] args) {


        System.out.println("Triệu Yến Vi".matches(".*[ÀÁÂÃÈÉÊÌÍÒÓÔÕÙÚĂĐĨŨƠàáâãèéêìíòóôõùúăđĩũơƯĂẠẢẤẦẨẪẬẮẰẲẴẶẸẺẼỀỀỂẾưăạảấầẩẫậắằẳẵặẹẻẽềềểếỄỆỈỊỌỎỐỒỔỖỘỚỜỞỠỢỤỦỨỪễệỉịọỏốồổỗộớờởỡợụủứừỬỮỰỲỴÝỶỸửữựỳỵỷỹ\u0300\u0301\u0302\u0303\u0306\u0309\u031b\u0323].*"));
        Scanner input = new Scanner(System.in);
        System.out.println("*** HANGUL ROMANIZER ***");
        String kr = "김성민(j'aime beaucoup le kimchi)";
        System.out.println("Demo :\n" + kr + "\ndevient \""+ romanize(kr)+ "\"");
        System.out.println("\nA vous d'essayer ! Entrez une phrase en coréen : ");
        String kr_sentence = input.nextLine();

        String romanization = romanize(kr_sentence);

        System.out.println(romanization);
        input.close();

    }//fin main
    public static String romanize(String kSentence) {

        char[] charArray = kSentence.toCharArray();

        String romanization = "";

        for (char kr : charArray) {

            KSyllable kSyllable = allSyllables.get((int) kr);
            if (kSyllable == null) {
                romanization += kr;
            } else {
                int initial = kSyllable.initial;
                int medial = kSyllable.medial;
                int finale = kSyllable.finale;

                romanization += allInitial.get(initial);
                romanization += allMedial.get(medial);
                romanization += allFinal.get(finale);
            } //fin if-else

        }//fin for
        return romanization;
    }// fin romanize

    private static Map<Integer, KSyllable> generateAllSyllables() {
        Map<Integer, KSyllable> result = new HashMap<>();

        for (int initial = 0; initial <= 18; initial++){
            for (int medial = 0; medial <= 20; medial++){
                for (int finale = 0; finale <= 27; finale++){
                    // voir "https://en.wikipedia.org/wiki/Korean_language_and_computers#Hangul_in_Unicode"
                    int syllableCode = initial * 588 + medial * 28 + finale + 44032;
                    result.put(syllableCode, new KSyllable(initial, medial, finale));
                }//fin for finale
            }//fin for medial
        }// fin for initial

        return result;
    }// fin generateAllSyllables

    private static Map<Integer, String> loadRomanization(String fileName) {
        Map<Integer, String> result = new HashMap<>();

        try {
            Files.lines(Paths.get(Hangul.class.getResource(fileName).toString().substring(6)))
                    //exp lambda
                    .forEach(l ->  {
                        String[] split = l.split("=", -1); // "-1" pour garder les blancs (=> chaines vides)
                        result.put(Integer.parseInt(split[0]), split[1]);
                    });
        } catch (IOException e) {
            // Fichier introuvable!
            System.out.println("Fichier introuvable.");
        }

        return result;
    }//fin loadRomanization


}
